package com.xtm.exercise.base.A12threadDemo.ThreadSafe.test;

public class Ticket implements Runnable {
	//多个对象 共享同一个资源， 需要static修饰
	private int ticket = 100;
	//创建锁对象(可以是任意对象)
	private Object obj = new Object();

	@Override
	public void run() {
		while (true) {
			synchronized ( obj ){  //任意字符串
				if (ticket >0) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println(Thread.currentThread().getName()+"：正在卖第 "+ ticket-- +"张票");
				}
			}
		}
	}
}
